<h1 id="RawMemory"><a href="#RawMemory" class="headerlink" title="RawMemory"></a>RawMemory</h1><p>RawMemory 对象允许您实现自己的内存序列器来替代内建的基于 <code>JSON.stringify</code> 的序列器。
它还允许使用异步内存分段功能请求最多 10 MB 的额外内存。</p>
<p>您也可以通过下述方法访问其他玩家的内存分段。</p>
<h2 id="RawMemory.segments" class="api-property api-property--property  "><span class="api-property__name">RawMemory.segments</span><span class="api-property__type">object</span></h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setActiveSegments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 在下个 tick</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>RawMemory<span class="token punctuation">.</span>segments<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>RawMemory<span class="token punctuation">.</span>segments<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
RawMemory<span class="token punctuation">.</span>segments<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'{"foo": "bar", "counter": 15}'</span><span class="token punctuation">;</span></code></pre><p>当前 tick 可用的异步内存分段对象。对象的每个键都是其对应字符串数据的分段 ID。使用 <a href="#RawMemory.setActiveSegments"><code>setActiveSegments</code></a> 来在下个 tick 获取分段。分段数据在 tick 结束时会自动保存。每个分段的最大容量是 100 KB。</p>
<h2 id="RawMemory.foreignSegment" class="api-property api-property--property  "><span class="api-property__name">RawMemory.foreignSegment</span><span class="api-property__type">object</span></h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setActiveForeignSegment</span><span class="token punctuation">(</span><span class="token string">'player'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 在下个 tick</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>RawMemory<span class="token punctuation">.</span>foreignSegment<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">// --> {"username": "player", "id": 40, "data": "Hello!"} </span>
</code></pre><p>当前 tick 可用的其他玩家的内存分段对象。使用 <a href="#RawMemory.setActiveForeignSegment"><code>setActiveForeignSegment</code></a> 来在下个 tick 获取分段。
该对象包含以下属性：</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>username</code></td><td>string</td><td><p>其他玩家的名称</p>
</td>
</tr><tr><td><code>id</code></td><td>number</td><td><p>被请求的内存分段 ID。</p>
</td>
</tr><tr><td><code>data</code></td><td>string</td><td><p>分段的内容。</p>
</td>
</tr></tbody></table>

<h2 id="RawMemory.interShardSegment" class="api-property api-property--property  api-property--deprecated"><span class="api-property__name">RawMemory.interShardSegment</span><span class="api-property__type">string</span></h2><div class="api-deprecated"><p>此属性已被弃用，将很快删除。 请使用 <a href="#InterShardMemory"><code>InterShardMemory</code></a> 代替。</p>
</div>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span>interShardSegment <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    creeps<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        Bob<span class="token punctuation">:</span> <span class="token punctuation">{</span>role<span class="token punctuation">:</span> <span class="token string">'claimer'</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 在其他 shard</span>
<span class="token keyword">var</span> interShardData <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>RawMemory<span class="token punctuation">.</span>interShardSegment<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>interShardData<span class="token punctuation">.</span>creeps<span class="token punctuation">[</span>creep<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    creep<span class="token punctuation">.</span>memory <span class="token operator">=</span> interShardData<span class="token punctuation">[</span>creep<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">delete</span> interShardData<span class="token punctuation">.</span>creeps<span class="token punctuation">[</span>creep<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
RawMemory<span class="token punctuation">.</span>interShardSegment <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>interShardData<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在所有世界 shard 都可以访问的内存分段字符串。字符串的最大长度为 100 KB。</p>
<p><strong>警告:</strong> 该分段不能同时使用！所有的 shard 都有同一个数据实例的访问权限。
当分段内容被两个 shard 同时写入时，由于分段字符串写入的原子性，您可能会丢失一些数据。
您必须实现自己的系统来确定何时允许不同的 shard 重写内存分段。例如基于 <a href="https://en.wikipedia.org/wiki/Mutual_exclusion" target="_blank" rel="noopener">互斥锁（mutual exclusions）</a>。  </p>
<h2 id="RawMemory.get" class="api-property api-property--method  "><span class="api-property__name">RawMemory.get</span><span class="api-property__args">()</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> myMemory <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>RawMemory<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>获取 <code>Memory</code> 对象的原始字符串形式。</p>
<h3 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h3><p>返回一个字符串值。</p>
<h2 id="RawMemory.set" class="api-property api-property--method  "><span class="api-property__name">RawMemory.set</span><span class="api-property__args">(value)</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>myMemory<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>设置新的 <code>Memory</code> 值。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>value</code></td><td>string</td><td><p>新的字符串形式的 memroy 值。</p>
</td>
</tr></tbody></table>

<h2 id="RawMemory.setActiveSegments" class="api-property api-property--method  "><span class="api-property__name">RawMemory.setActiveSegments</span><span class="api-property__args">(ids)</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setActiveSegments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>使用多个分段数组的 ID 来请求响应的内存分段。内存分段将在下个 tick 的 <a href="#RawMemory.segments"><code>segments</code></a> 对象中可用。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>ids</code></td><td>array</td><td><p>分段的 ID 数组。每个 ID 都应是从 0 到 99 之间的一个整数。同一时间最多只能启用 10 个分段。后续的 <code>setActiveSegments</code> 调用将覆盖先前的调用。</p>
</td>
</tr></tbody></table>

<h2 id="RawMemory.setActiveForeignSegment" class="api-property api-property--method  "><span class="api-property__name">RawMemory.setActiveForeignSegment</span><span class="api-property__args">(username, [id])</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setActiveForeignSegment</span><span class="token punctuation">(</span><span class="token string">'player'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setActiveForeignSegment</span><span class="token punctuation">(</span><span class="token string">'player'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setActiveForeignSegment</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>请求其他用户的内存分段。该分段应被其所有者使用 <a href="#RawMemory.setPublicSegments"><code>setPublicSegments</code></a> 标注为公开。
该分段数据将在下个 tick 的 <a href="#RawMemory.foreignSegment"><code>foreignSegment</code></a> 对象中可用。
您同一时间只能访问一个外部分段。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>username</code></td><td>string | null</td><td><p>其他用户的名称。传入 <code>null</code> 来清除外部分段。</p>
</td>
</tr><tr><td><code>id (可选)</code></td><td>number</td><td><p>0 到 99 之间的请求分段 ID。如果未定义，将请求该用户通过 <a href="#RawMemory.setDefaultPublicSegment"><code>setDefaultPublicSegment</code></a> 设置的默认公开分段。</p>
</td>
</tr></tbody></table>

<h2 id="RawMemory.setDefaultPublicSegment" class="api-property api-property--method  "><span class="api-property__name">RawMemory.setDefaultPublicSegment</span><span class="api-property__args">(id)</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setPublicSegments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">21</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
RawMemory<span class="token punctuation">.</span><span class="token function">setDefaultPublicSegment</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setDefaultPublicSegment</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>将指定的内存分段设置为您的默认公开分段。当其他玩家使用 <a href="#RawMemory.setActiveForeignSegment"><code>setActiveForeignSegment</code></a> 访问您的分段并且没有传入 <code>id</code> 参数时返回该分段。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>id</code></td><td>number | null</td><td><p>0 到 99 之间的内存分段 ID。传入 <code>null</code> 来移除您的默认公开分段。</p>
</td>
</tr></tbody></table>

<h2 id="RawMemory.setPublicSegments" class="api-property api-property--method  "><span class="api-property__name">RawMemory.setPublicSegments</span><span class="api-property__args">(ids)</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setPublicSegments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code>RawMemory<span class="token punctuation">.</span><span class="token function">setPublicSegments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>将指定的分段设置为公开。将允许其他用户使用 <a href="#RawMemory.setActiveForeignSegment"><code>setActiveForeignSegment</code></a> 来访问它们。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>ids</code></td><td>array</td><td><p>分段 ID 数组。每个 ID 都应是从 0 到 99 之间的一个整数。后续的 <code>setPublicSegments</code> 调用将覆盖先前的调用。</p>
</td>
</tr></tbody></table>

